package com.resou.reader.discovery.choicenes.fragment;

import android.util.Log;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.ChoiceneBookListBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.UserUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 书单P层
 * CreateTime: 2019-03 -22
 **/
public class ChoiceneBookPresenter extends ResouBasePresenter<IChoiceneBookFragmentContract.IChoiceneBookView> implements IChoiceneBookFragmentContract.IChoiceneBookPresenter {


    StoreService storeService;

    public ChoiceneBookPresenter(IChoiceneBookFragmentContract.IChoiceneBookView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    @Override
    public void quearChoiceneBookData(String gender, String type, String category, String label, int page) {
        storeService.quearBookList(gender, type, category, label, page, pageSize, UserUtil.getToken())
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<ChoiceneBookListBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<ChoiceneBookListBean> commonDataResult) {
                        if (null != commonDataResult.getData()) {
                            /**
                             * 推荐产品
                             */
                            if (0 != commonDataResult.getData().getRecommend().size()) {
                                mView.showShowRecommendBean(commonDataResult.getData().getRecommend());
                            } else {
                                mView.recommendEmpty();
                            }

                            int totalNum =
                                    commonDataResult.getData().getUnRecommend().getTotalPage() * pageSize;
                            int localNum = (page + 1) * pageSize;
                            /***
                             * 普通产品
                             */

                            if (localNum < totalNum) {
                                mView.showShowUnRecommendBean(commonDataResult.getData().getUnRecommend().getBookList());
                                mView.getAdapter().loadMoreComplete();
                            } else {
                                if (totalNum == 0) {
                                    mView.showEmpty();
                                }
                                mView.getAdapter().loadMoreEnd();
                                mView.showShowUnRecommendBean(commonDataResult.getData().getUnRecommend().getBookList());
                            }

                        } else {
                            mView.showEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.getSmartRefreshLayout().finishRefresh();
                        mView.getAdapter().loadMoreFail();
                        mView.showError(e);
                        Log.i("错误", "onError: " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
